package com.bb.blog.code.jvm;

import java.io.FileInputStream;

public class MyClassLoader extends ClassLoader {
    private String classPath;

    public MyClassLoader(String path) {
        this.classPath = path;

    }

    private byte[] loadByte(String className) throws Exception {
        className = className.replaceAll("\\.", "/");
        FileInputStream fis = new FileInputStream(classPath + "/" + className + ".class");
        int len = fis.available();
        byte[] data = new byte[len];
        fis.read(data);
        fis.close();
        return data;
    }

    protected Class<?> findClass(String className) throws ClassNotFoundException {
        try {
            byte[] bytes = loadByte(className);
            return defineClass(className, bytes, 0, bytes.length);
        } catch (Exception e) {
            throw new ClassNotFoundException();
        }
    }



    protected void test() {

    }

    public static void main(String[] args) throws ClassNotFoundException {

//      System.out.println(First.class.getClassLoader());
//        String className = First.class.getName();
//        System.out.println(className);
        MyClassLoader myClassLoader = new MyClassLoader("E:/");
        myClassLoader.test();
//        System.out.println(myClassLoader.getClass().getClassLoader());
//        myClassLoader.loadClass(className);
    }
}
